﻿using System;

namespace DemoWin.USBDriver
{

    /// <summary>
    /// USB通信封装类
    /// </summary>
    public class HidCommonHelper
    {
        #region parameter Define
        private HIDInterface hid = new HIDInterface();


        struct connectStatusStruct
        {
            public bool preStatus;
            public bool curStatus;
        }

        connectStatusStruct connectStatus = new connectStatusStruct();

        //推送连接状态信息
        public delegate void isConnectedDelegate(bool isConnected);
        public isConnectedDelegate isConnectedFunc;

        //推送接收数据信息
        public delegate void PushReceiveDataDele(byte[] datas);
        public PushReceiveDataDele pushReceiveData;

        #endregion

        /// <summary>
        /// 第一步需要初始化，传入vid、pid，并开启自动连接
        /// </summary>
        /// <param name="vID">vID</param>
        /// <param name="pID">pID</param>
        /// <param name="pushReceiveData"> 接收数据事件</param>
        /// <param name="isConnectedFunc">连接状态事件</param>
        public void HidInitial(UInt16 vID, UInt16 pID, PushReceiveDataDele pushReceiveData, isConnectedDelegate isConnectedFunc)
        {
            hid.StatusConnected = StatusConnected;
            hid.DataReceived = DataReceived;
            this.pushReceiveData = pushReceiveData;
            this.isConnectedFunc = isConnectedFunc;

            HIDInterface.HidDevice hidDevice = new HIDInterface.HidDevice();
            hidDevice.vID = vID;
            hidDevice.pID = pID;
            hidDevice.serial = "";
            hid.AutoConnect(hidDevice);
        }

        /// <summary>
        /// 连接状态
        /// </summary>
        /// <param name="isConnected"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void ConnectedFunc(bool isConnected)
        {
            try
            {
                Console.WriteLine("连接状态：" + isConnected);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 接收数据
        /// </summary>
        /// <param name="res"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void ReceiveData(byte[] res)
        {
            try
            {
                Console.WriteLine("接收数据：" + String.Join(" ", res));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //不使用则关闭
        public void Close()
        {
            hid.StopAutoConnect();
        }

        //发送数据
        public bool SendBytes(byte[] data)
        {

            return hid.Send(data);

        }

        //接受到数据
        public void DataReceived(object sender, byte[] e)
        {
            if (pushReceiveData != null)
                pushReceiveData(e);
        }

        //状态改变接收
        public void StatusConnected(object sender, bool isConnect)
        {
            connectStatus.curStatus = isConnect;
            if (connectStatus.curStatus == connectStatus.preStatus)  //connect
                return;
            connectStatus.preStatus = connectStatus.curStatus;

            if (connectStatus.curStatus)
            {
                isConnectedFunc(true);
                //Console.WriteLine("连接成功");
            }
            else //disconnect
            {
                isConnectedFunc(false);
                //Console.WriteLine("无法连接");
            }
        }

    }
}
